<?php
class CodtransController extends AppController {

	var $name = 'Codtrans';
	var $helpers = array('Csv');
	var $paginate = array('limit' => 50);
	
	function admin_index() {
		$this->Codtran->recursive = 0;
		$this->set('codtrans', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid codtran', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('codtran', $this->Codtran->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			if ($this->data['Codtran']['Date']['day'] == "") {
                $this->data['Codtran']['Date']['day'] = '00';
                $this->data['Codtran']['Date']['year'] = '0000';
                $this->data['Codtran']['Date']['month'] = '00';
            }
			$this->Codtranc->Director->recursive=-1;
			if(isset($this->data['Codtran']['director_id'])){
				$dir=$this->Codtran->Director->findById($this->data['Codtran']['director_id']);
				$this->data['Codtran']['Name']=$dir['Director']['name'];
			}
			if(isset($this->data['Codtran']['Name'])){
				$this->loadModel("Director");
				$dir=$this->Director->findByName($this->data['Codtran']['Name']);
				if($dir['Director']['id']){
					$this->data['Codtran']['director_id']=$dir['Director']['id'];
				}else{
					$director['Director']['name']=$this->data['Codtran']['Name'];
					$this->Director->create();
					$this->Director->save($director);
					$this->data['Codtran']['director_id']=$this->Director->getLastInsertId();
				}
			}
			
			$this->Codtran->create();
			if ($this->Codtran->save($this->data)) {
				$this->Session->setFlash(__('The codtran has been saved', true));				
				$this->redirect($_SERVER['HTTP_REFERER']);					
				//$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The codtran could not be saved. Please, try again.', true));
			}
		}
		$codetails = $this->Codtran->Codetail->find('list');
		$directors = $this->Codtran->Director->find('list');
		$this->set(compact('codetails','directors'));
	}
	function admin_add_trans($id=null) {
		$this->layout=false;
		if (!empty($this->data)) {
			//if($this->data['Codtran']['Date']['day']=="")
			//$this->data['Codtran']['Date']='0';
			//pr($this->data);exit;
			$this->Codtran->create();
			if ($this->Codtran->save($this->data)) {
				$this->Session->setFlash(__('The codtran has been saved', true));				
				$this->redirect($_SERVER['HTTP_REFERER']);					
				//$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The codtran could not be saved. Please, try again.', true));
			}
		}
		$this->loadModel("Codexec");
		$codetails = $this->Codexec->findById($id);
		$directors = $this->Codexec->Director->find('list');
                
		$this->set(compact('codetails','directors'));		
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid codtran', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$dir['Director']['name']=$this->data['Codtran']['Name'];
			$dir['Director']['id']=$this->data['Codtran']['director_id'];
			//pr($this->data);//exit;
			if ($this->Codtran->save($this->data)) {
				$this->Codtran->Director->save($dir);
				$this->Session->setFlash(__('The codtran has been saved', true));
				$this->redirect($_SERVER['HTTP_REFERER']);
				//$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The codtran could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Codtran->read(null, $id);
		}
		$codetails = $this->Codtran->Codetail->find('list');
		$directors = $this->Codtran->Director->find('list');
		$this->set(compact('codetails','directors'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for codtran', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Codtran->delete($id)) {
			$this->Session->setFlash(__('Codtran deleted', true));
			if(isset($_SERVER['HTTP_REFERER'])){
				$this->redirect($_SERVER['HTTP_REFERER']);
				}
				//$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Codtran was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_export()
        {
           	$this->Codtran->recursive=-1;
            // Find fields needed without recursing through associated models
            $codtrans = $this->Codtran->find(
                'all',
                array('order' => "Codtran.id ASC",'contain' => false));
            $this->layout = false;
			$this->autoLayout = false;			
			$this->set('codtrans',$codtrans);
        } 
	function admin_upload_csv()	{
		if (!empty($this->data)) {	
		//pr($this->data);
		if (!empty($this->data['Codtran']['fileName']['name']) && is_uploaded_file($this->data['Codtran']['fileName']['tmp_name']))
			{
				if($this->data['Codtran']['fileName']['type']=="text/x-csv" OR $this->data['Codtran']['fileName']['type']=="application/octet-stream" OR $this->data['Codtran']['fileName']['type']=="application/vnd.ms-excel"){
					//$fileData = fread(fopen(, "r"),$this->data['Coevent']['fileName']['size']);
					$messages = $this->Codtran->import($this->data['Codtran']['fileName']['tmp_name']);
					//debug($messages);
					if(isset($messages['errors']) && count($messages['errors'])>0){
					$msg=implode("<br>",$messages['errors']);
					}else
					{
					$msg=implode("<br>",$messages['messages']);
					}
					//echo $msg;
					$this->Session->setFlash($msg);
				}else{
					$this->Session->setFlash(__('Invalid file type. You must upload a CSV file.', true));
				}
			}
		else{
			$this->Session->setFlash(__('CSV File Required.', true));
		}
		}
	}	
}
